const LApp = getApp();
// pages/operat/origins/originTaskEdit/originTaskEdit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgSrc: [],
    nodeModel: {},
    address:"",
    videoSrc:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let nodeModel = LApp.pages.get("origins/originHandleTask/originHandleTask").data.nodesModel;
     this.setData({
       nodeModel: nodeModel,
       imgSrc: [[nodeModel.ImgUrl]]
     })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //提交信息
  goOriginSuccess: function (e) {
    var tempFilePath = "";
    if (this.data.imgSrc[0]){
       tempFilePath = this.data.imgSrc[0];
    }
    if (this.data.videoSrc[0]) {
      var tempFilePath = this.data.videoSrc[0];
    }
    let _this = this;
    if (tempFilePath[0] != this.data.nodeModel.ImgUrl)
    { //上传文件
      LApp.uploadFile({
        url: 'nbapi/comm/file/upload',
        filepath: tempFilePath[0],
        file: "files",
        success: function (r) {
          if (r.success == true) {
            _this.data.nodeModel.ImgUrl = r.result;
            _this.submitt();
          }
        }
      })}
   else{
      _this.submitt();
   }

  },

  //点击选择
  upload: function () {
    var uploadType = this.data.nodeModel.Display;
    // if (uploadType == "0,1") {
    //   this.chooseImg();
    // }
    if (uploadType == "0,2") {
      this.chooseVedio();
    }
    else{
      this.chooseImg();
    }
  },
  //上传小视频
  chooseVedio: function () {
    var that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success: function (res) {
        that.setData({
          videoSrc: res.tempFilePath
        })
      }
    })
  },
  chooseImg: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
      //  that.data.imgSrc.push(res.tempFilePaths);
        that.data.imgSrc[0] = res.tempFilePaths
        that.setData({
          imgSrc: that.data.imgSrc
        })
      }
    })
  },
  bindContent: function (e) {

    wx.$$apply(e, this.data.nodeModel);
  },
  submitt:function(){
    let _this = this;
    _this.data.nodeModel.ChangeLoginID = LApp.globalData.userInfo.UserID;
    _this.data.nodeModel.ChangeLoginName = LApp.globalData.userInfo.RealName;
    var model = _this.data.nodeModel;
    LApp._post({
      url: 'nbapi/originstask/origins/addnodes',
      data: model,
      success: function (r) {
        if(r==true)
        {
          //操作成功后跳转
          LApp.openView("../originSuccess/originSuccess");
        }
        else
        {
          wx.showModal({
            title: '提示',
            content: '保存失败',
            showCancel: false
          })
        }
      }
    })
  },
  getLoction: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          name: res.name,
          address: res.address,
        })
      },
    })
  },
  
})